package com.lzx.adx.report.support.strategy.v2.channel;

import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
@Slf4j
@ProductType(ProductChannelConstant.XHS)
public class XhsReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        long time = System.currentTimeMillis();
        Map<String,String> map=new LinkedHashMap<>();

        map.put("platform","ANDROID");
        map.put("ts_ms",String.valueOf(time));
        map.put("click_id",reportDTO.getBizRequestId());
        map.put("ip", reportDTO.getIp());

        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) &&!StringUtils.contains(reportDTO.getImeiSum(),"__")){
            map.put("imei_sum", reportDTO.getImeiSum());
        }

        if(StringUtils.isNotBlank(reportDTO.getOaid()) &&!StringUtils.contains(reportDTO.getOaid(),"__")){
            map.put("oaid", reportDTO.getOaid());
            map.put("oaid_sum",SecureUtil.md5(reportDTO.getOaid()));
        }

        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) &&!StringUtils.contains(reportDTO.getOaidSum(),"__")){
            map.put("oaid_sum", reportDTO.getOaidSum());
        }

        if(StringUtils.isNotBlank(reportDTO.getHash_android_id()) &&!StringUtils.contains(reportDTO.getHash_android_id(),"__")){
            map.put("android_id_sum", reportDTO.getHash_android_id());
        }

        if(StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            map.put("ua", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        }

        String allCabackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        log.info("完整的回调地址{}",allCabackUrl);
        map.put("callback_url",allCabackUrl);

        String params = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
        return  accountDTO.getDspReportUrl()+"?"+params;
    }
}
